package com.zj.utils.utils.http;
import android.util.Log;

import com.blankj.utilcode.util.SPUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.StringReader;
import java.util.LinkedHashMap;
import static com.zj.utils.utils.http.HttpConfig.callService;

/**
 * 当token失效时用来重新获取token
 * Created by Jiang on 2019/1/30
 */
public class GetToken {
    private static final GetToken ourInstance = new GetToken();

    private GetToken() {
    }

    public static GetToken getInstance() {
        return ourInstance;
    }

    public Boolean initData(String name, String pass) {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        //1.登陆接口 POST
        map.put("Username", name);
        map.put("Password", pass);
        String result = callService(0,"getwebuser", map);
        Log.e("TAG", "initData: " + result);
        return parseXMLWithPull(result);
    }


    //1.TODO 用户登陆解析用Pull方式解析XML
    private Boolean parseXMLWithPull(String xmlData) {
        int res = 1;
        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            //设置输入的内容
            xmlPullParser.setInput(new StringReader(xmlData));
            //获取当前解析事件，返回的是数字
            int eventType = xmlPullParser.getEventType();
            //保存内容
            String msg;
            String usersId;
            String name;

            while (eventType != (XmlPullParser.END_DOCUMENT)) {
                String nodeName = xmlPullParser.getName();
                switch (eventType) {
                    //开始解析XML
                    case XmlPullParser.START_TAG: {
                        //nextText()用于获取结点内的具体内容
                        if ("usersId".equals(nodeName)) {
                            usersId = xmlPullParser.nextText();
                            //SharedPreferencesUtils.putString(getApplicationContext(), "userId", usersId);
                            com.blankj.utilcode.util.SPUtils.getInstance().put("userId", usersId);
                        }
                        if ("msg".equals(nodeName)) {
                            msg = xmlPullParser.nextText();

                            if (msg.equals("成功")) {
                                com.blankj.utilcode.util.SPUtils.getInstance().put("islogin", "1");
                                res = 0;
                            }
                        }
                        if ("token".equals(nodeName)) {
                            String token = xmlPullParser.nextText();
                            com.blankj.utilcode.util.SPUtils.getInstance().put("token", token);
                        }
                        if ("Name".equals(nodeName)) {
                            name = xmlPullParser.nextText();
                            com.blankj.utilcode.util.SPUtils.getInstance().put("name", name);
                        }

                        /*else if("version".equals(nodeName))
                            version = xmlPullParser.nextText();*/
                    }
                    break;
                    //结束解析
                    case XmlPullParser.END_TAG: {
                    }
                    break;
                    default:
                        break;
                }
                //下一个
                eventType = xmlPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res == 0;
    }


    /**
     * 重新调取登录接口，获取新的token
     *
     * @return 新token
     */
    public String setToken() {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        //1.登陆接口 POST
        map.put("Username", com.blankj.utilcode.util.SPUtils.getInstance().getString("mUsername"));
        map.put("Password", com.blankj.utilcode.util.SPUtils.getInstance().getString("mPassword"));
        String result = callService(0,"getwebuser", map);
        return parseXMLWithPulls(result);
    }


    //1.TODO 用户登陆解析用Pull方式解析XML
    private String parseXMLWithPulls(String xmlData) {
        String token = null;
        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            //设置输入的内容
            xmlPullParser.setInput(new StringReader(xmlData));
            //获取当前解析事件，返回的是数字
            int eventType = xmlPullParser.getEventType();
            //保存内容
            String msg;
            String usersId;
            String name;


            while (eventType != (XmlPullParser.END_DOCUMENT)) {
                String nodeName = xmlPullParser.getName();
                switch (eventType) {
                    //开始解析XML
                    case XmlPullParser.START_TAG: {
                        //nextText()用于获取结点内的具体内容
                        if ("usersId".equals(nodeName)) {
                            usersId = xmlPullParser.nextText();
                            //SharedPreferencesUtils.putString(getApplicationContext(), "userId", usersId);
                            com.blankj.utilcode.util.SPUtils.getInstance().put("userId", usersId);
                        }
                        if ("msg".equals(nodeName)) {
                            msg = xmlPullParser.nextText();

                            if (msg.equals("成功")) {
                                com.blankj.utilcode.util.SPUtils.getInstance().put("islogin", "1");
                            }
                        }
                        if ("token".equals(nodeName)) {
                            token = xmlPullParser.nextText();
                            com.blankj.utilcode.util.SPUtils.getInstance().put("token", token);
                        }
                        if ("Name".equals(nodeName)) {
                            name = xmlPullParser.nextText();
                            com.blankj.utilcode.util.SPUtils.getInstance().put("name", name);
                        }

                        /*else if("version".equals(nodeName))
                            version = xmlPullParser.nextText();*/
                    }
                    break;
                    //结束解析
                    case XmlPullParser.END_TAG: {
                    }
                    break;
                    default:
                        break;
                }
                //下一个
                eventType = xmlPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }


    /**
     * 判断token是否可用
     *
     * @return 是否
     */
    public Boolean getToken() {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        //1.登陆接口 POST
        map.put("token", SPUtils.getInstance().getString("token"));
        String result = callService(1,"verifyToken", map);
        return get(result);
    }


    //1.TODO 用户登陆解析用Pull方式解析XML
    private Boolean get(String xmlData) {
        int res = 1;
        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            //设置输入的内容
            xmlPullParser.setInput(new StringReader(xmlData));
            //获取当前解析事件，返回的是数字
            int eventType = xmlPullParser.getEventType();
            //保存内容
            String msg = null;
            String status;

            while (eventType != (XmlPullParser.END_DOCUMENT)) {
                String nodeName = xmlPullParser.getName();
                switch (eventType) {
                    //开始解析XML
                    case XmlPullParser.START_TAG: {
                        //nextText()用于获取结点内的具体内容
                        if ("msg".equals(nodeName)) {
                            msg = xmlPullParser.nextText();
                        }
                        if ("Status".equals(nodeName)) {
                            status = xmlPullParser.nextText();
                            assert msg != null;
                            if (msg.equals("成功") && status.equals("true")) {
                                res = 0;
                            }
                        }
                    }
                    break;
                    //结束解析
                    case XmlPullParser.END_TAG: {
                    }
                    break;
                    default:
                        break;
                }
                //下一个
                eventType = xmlPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res == 0;
    }


}
